package com.hzlj.position.dbsync.common.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.redis.annotation.SyncLock;
import com.google.common.collect.Lists;
import com.hzlj.position.dbsync.common.common.dto.syncOffsetConfig.SyncOffsetConfigQueryResultDTO;
import com.hzlj.position.dbsync.common.common.dto.syncOffsetConfig.SyncOffsetConfigUpsertDTO;
import com.hzlj.position.dbsync.common.common.model.SyncOffsetConfig;
import com.hzlj.position.dbsync.common.dao.SyncOffsetConfigDao;
import com.hzlj.position.dbsync.common.service.SyncOffsetConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 同步offset配置表(SyncOffsetConfig)表服务接口
 *
 * @author lifh
 * @date 2023-11-30 16:00:38
 */
@Slf4j
@Service
public class SyncOffsetConfigServiceImpl implements SyncOffsetConfigService {
    @Resource
    private SyncOffsetConfigDao syncOffsetConfigDao;

    @Override
    public void upsert(String jgCode, String syncType, Date syncTime) {
        jgCode = ObjectUtil.defaultIfEmpty(jgCode, "1");
        SyncOffsetConfigUpsertDTO upsert = new SyncOffsetConfigUpsertDTO();
        upsert.setJgCode(jgCode);
        upsert.setSyncType(syncType);
        upsert.setLatestTime(syncTime);
        upsert(
                Lists.newArrayList(upsert)
        );
    }

    @Override
    public void upsert(String jgCode, String syncType, String syncOffset) {
        jgCode = ObjectUtil.defaultIfEmpty(jgCode, "1");
        SyncOffsetConfigUpsertDTO upsert = new SyncOffsetConfigUpsertDTO();
        upsert.setJgCode(jgCode);
        upsert.setSyncType(syncType);
        upsert.setLatestOffset(syncOffset);
        upsert(
                Lists.newArrayList(upsert)
        );
    }


    @Override
    @SyncLock
    public void upsert(Collection<SyncOffsetConfigUpsertDTO> dtos) {
        if (ObjectUtil.isEmpty(dtos)) {
            return;
        }

        Date now = new Date();
        List<SyncOffsetConfig> syncs = dtos.stream()
                .map(e -> BeanUtil.copyProperties(e, SyncOffsetConfig.class))
                .collect(Collectors.toList());

        for (SyncOffsetConfig sync : syncs) {
            SyncOffsetConfigQueryResultDTO exists
                    = this.syncOffsetConfigDao.getSyncOffsetConfig(sync.getJgCode(),sync.getSyncType());
            sync.setUpdateTime(now);
            //更新
            if (ObjectUtil.isNotEmpty(exists)) {
                this.syncOffsetConfigDao.updateSyncOffsetConfig(sync);
                continue;
            }
            sync.setCreateTime(now);
            this.syncOffsetConfigDao.save(sync);
        }
    }

    @Override
    public Date getLatestTime(String jgCode, String syncType) {
        SyncOffsetConfigQueryResultDTO exists
                = this.syncOffsetConfigDao.getSyncOffsetConfig(jgCode,syncType);
        if (ObjectUtil.isEmpty(exists)) {
            return null;
        }
        return exists.getLatestTime();
    }

    @Override
    public String getLatestOffset(String jgCode, String syncType) {
        SyncOffsetConfigQueryResultDTO exists
                = this.syncOffsetConfigDao.getSyncOffsetConfig(jgCode,syncType);
        if (ObjectUtil.isEmpty(exists)) {
            return null;
        }
        return exists.getLatestOffset();
    }
}
